package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.*;
import com.atguigu.spzx.manager.utils.PageUtils;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.base.BaseEntity;
import com.atguigu.spzx.model.entity.product.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.spzx.manager.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Windows
 * @description 针对表【product(商品)】的数据库操作Service实现
 * @createDate 2024-02-01 10:18:02
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;


    @Override
    public Page<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {

        List<Product> productList = this.baseMapper.findByPage(productDto);
        Page<Product> productPage = PageUtils.listToPage(productList, page, limit);


        return productPage;
    }

    @Transactional
    @Override
    public void saveProduct(Product product) {
        // 1、保存product基本信息
        product.setStatus(0);
        product.setAuditStatus(0);
        this.save(product);

        // 2、保存productSku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            // 构建skuCode “1_1”
            productSku.setSkuCode(product.getId() + "_" + i);
            // 设置商品id
            productSku.setProductId(product.getId());
            // SKU名就是商品名加规格名（最小库存单位)
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            // 销量初始值
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSku.setStatus(0);

            productSkuMapper.insert(productSku);
        }
        // 3、保存productDetail
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);
    }

    @Override
    public Product getProductById(Long id) {
        // 和保存一样，需要从product、productSku、ProductDetails三张表中获取数据

        Product product = this.baseMapper.getProductById(id);

        List<ProductSku> productSkuList = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, id));
        product.setProductSkuList(productSkuList);

        ProductDetails productDetails = productDetailsMapper.selectOne(new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId, id));
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return product;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateProductById(Product product) {
        // 1、同上，需要操作多表
        this.updateById(product);

        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> productSkuMapper.updateById(productSku));

        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.update(productDetails, new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId,product.getId()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        this.deleteById(id);
        productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getProductId, id));
        productDetailsMapper.delete(new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId, id));
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        if (auditStatus.equals(1)) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        }else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批未通过");
        }
        this.update(product,new LambdaQueryWrapper<Product>()
                .eq(BaseEntity::getId,id));
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        if (status.equals(1)) {
            product.setStatus(1);
        }else {
            product.setStatus(-1);
        }
        this.update(product,new LambdaQueryWrapper<Product>()
                .eq(BaseEntity::getId,id));
    }
}




